package com.ufaud.ais.sys.webapp.action;

import java.util.Date;

import com.ufaud.ais.sys.entity.Department;
import com.ufaud.ais.sys.entity.Employee;
import com.ufaud.ais.sys.entity.Role;
import com.ufaud.ais.sys.service.IEmployeeService;

/**
 * 
 * @author Chenjd
 * @version 最近的一次更新 2010-8-16
 * 
 */
public class SaveEmployeeAction {

	private String employeeCode;

	private String employeeName;

	private String email;

	private String job;

	private String departmentId;

	private String roleIds;

	private String accountName;
	private String accountPassword;

	// 以后可能开放setter
	private boolean enabled = true;
	private boolean accountExpired;
	private boolean accountLocked;
	private boolean credentialsExpired;

	private IEmployeeService employeeService;

	public String execute() {
		if (!validate()) {
			return "fail";
		}

		Employee aNewOne = new Employee();
		aNewOne.setEmployeeCode(employeeCode);
		aNewOne.setEmployeeName(employeeName);
		aNewOne.setEmail(email);
		aNewOne.setEmployDate(new Date());
		aNewOne.setJob(job);
		Department de = new Department();
		de.setDeptId(Long.parseLong(departmentId));
		aNewOne.setDepartment(de);
		aNewOne.setAccountName(accountName);
		accountPassword = "1234";
		aNewOne.setAccountPassword(accountPassword);

		String[] roleIdArr = roleIds.split(",");
		for (int i = 0; i < roleIdArr.length; i++) {
			Role role = new Role();
			role.setRoleId(Long.parseLong(roleIdArr[i]));
			aNewOne.addRole(role);
		}

		aNewOne.setEnabled(enabled);
		aNewOne.setAccountExpired(accountExpired);
		aNewOne.setAccountLocked(accountLocked);
		aNewOne.setCredentialsExpired(credentialsExpired);

		employeeService.save(aNewOne);

		return "success";
	}

	private boolean validate() {
		return true;
	}

	public void setEmployeeCode(String employeeCode) {
		this.employeeCode = employeeCode;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public void setDepartmentId(String departmentId) {
		this.departmentId = departmentId;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	public void setAccountPassword(String accountPassword) {
		this.accountPassword = accountPassword;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}

	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

}
